package com.rjet.access_control.socket;

import java.math.BigInteger;

public class HexUtil {


    private static String[] binaryArray =
            {"0000","0001","0010","0011",
                    "0100","0101","0110","0111",
                    "1000","1001","1010","1011",
                    "1100","1101","1110","1111"};

    /** Convert byte[] to hex string.这里我们可以将byte转换成int，然后利用Integer.toHexString(int)来转换成16进制字符串。
     * @param src byte[] data
     * @return hex string
     */
    public static String bytesToHexString(byte[] src){
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }


    /**
     * Convert hex string to byte[]
     * @param hexString the hex string
     * @return byte[]
     */
    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }


    /**
     * Convert char to byte
     * @param c char
     * @return byte
     */
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

    /**
     * 16进制转10进制
     * @return
     */
    public static int hexToDec(String strHex){

        return new BigInteger(strHex, 16).intValue();
    }

    /**
     * 16进制转2进制
     * @return
     */
    public static byte[] hexToBin(String hexString){

        if (hexString==null || hexString.length()==0){
            return null;
        }
        byte[] result=new byte[hexString.length()/2];
        for (int i=0,foot=0;i<hexString.length();i+=2,foot++){
            String temp=hexString.substring(i,i+2);//切割 16进制 二位为一个单位转换
            result[foot]=(byte) Integer.parseInt(temp,16);
        }

        return result;
    }


    /**
     *
     * @return 转换为二进制字符串
     */
    public static String bytes2BinaryStr(byte[] bArray){

        String outStr = "";
        int pos = 0;
        for(byte b:bArray){
            //高四位
            pos = (b&0xF0)>>4;
            outStr+=binaryArray[pos];
            //低四位
            pos=b&0x0F;
            outStr+=binaryArray[pos];
        }
        return outStr;
    }

    /**
     * ascii 转换为 hex
     * @param str
     * @return
     */
    public static String convertStringToHex(String str){

        char[] chars = str.toCharArray();

        StringBuffer hex = new StringBuffer();
        for(int i = 0; i < chars.length; i++){
            hex.append(Integer.toHexString((int)chars[i]));
        }

        return hex.toString();
    }

    /**
     * hex 转换为 ascii
     * @param hex
     * @return
     */
    public static String convertHexToString(String hex){

        hex = hex.replace(" ", "");

        StringBuilder sb = new StringBuilder();
        StringBuilder temp = new StringBuilder();

        //49204c6f7665204a617661 split into two characters 49, 20, 4c...
        for( int i=0; i<hex.length()-1; i+=2 ){

            //grab the hex in pairs
            String output = hex.substring(i, (i + 2));
            //convert hex to decimal
            int decimal = Integer.parseInt(output, 16);
            //convert the decimal to character
            sb.append((char)decimal);

            temp.append(decimal);
        }

        return sb.toString();
    }


    public static void main(String[] args) {

//        float sign = 80;
//        double distance = 0;
//
//        if (sign <= 8){
//            distance = 0;
//        }else if (sign > 8 && sign <= 20){
//            distance = (sign-8)/12 * 40;
//        }else {
//            distance = Math.pow(10, (sign-30)/25) * 100;
//        }
//
//        System.out.println(distance);

        System.out.println(HexUtil.convertHexToString(" 33 37 30 37 32 37 36 36 32 33"));
        System.out.println(HexUtil.convertHexToString(" 33 37 30 37 32 37 36 36 32 33"));
        System.out.println(HexUtil.convertStringToHex("3"));
        System.out.println(HexUtil.convertStringToHex("4"));
    }


}
